//
//  MYHttpTool.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-26.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYHttpTool.h"
#import "AFNetworking.h"

#define MYAccountPlace [MYDocument stringByAppendingPathComponent:@"aaa.plist"]

@implementation MYHttpTool
+ (void)POST:(NSString *)URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *))failure
{
    //创建请求管理者,拼接参数parameters
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    //发送POST请求
    [mgr POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}
+ (void)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
+ (void)upload:(NSString *)URLString parameters:(id)parameters uploadParam:(MYUploadParam *)uploadParam success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    // 发送post请求 上传图片只能用这个方法
    // pic	true	binary	要上传的图片，仅支持JPEG、GIF、PNG格式，图片大小小于5M。
    // 拼接一个二进制数据:网络传输中文件都是以二进制格式传输
    // 二进制文件不能当做普通参数拼接,http协议不支持这种拼接格式
    // 上传文件 一定用这个multipart
    [mgr POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:uploadParam.data name:uploadParam.paramName fileName:uploadParam.fileName mimeType:uploadParam.mimeType];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//监控当前网络
+ (void)monitorHttpStateChangedResult:(void (^)(NSString *))result
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    //当网络状态改变就会调用
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSString *resultStr;
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                resultStr = @"1未知网络";
                break;
            case AFNetworkReachabilityStatusNotReachable:
                resultStr = @"0没有网络(断网)";
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                resultStr = @"1手机自动网络";
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                resultStr = @"1WIFI";
                break;
            default:
                resultStr = @"0网络异常，请检查网络设置!";
                break;
        }
        if (result) {
            result(resultStr);
        }
    }];
    //开始监控
    [mgr startMonitoring];
}
@end
